package com.diandian.pos.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by jakehu on 2015/11/12.
 */
public class CustomizedPropertyConfigurer extends PropertyPlaceholderConfigurer {
    private static Map<String,String> ctxPropertiesMap;
    
    private static final Logger logger = LoggerFactory.getLogger(CustomizedPropertyConfigurer.class);
    public static boolean isBroadType = false;
    public static List<String> mqList = null;

    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactoryToProcess,
            Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        ctxPropertiesMap = new HashMap<String,String>();
        mqList = new ArrayList<String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            ctxPropertiesMap.put(keyStr, value);
        }
        //
        isBroadType = Boolean.parseBoolean((String)ctxPropertiesMap.get("is_broad_type"));
        String mqStr = ctxPropertiesMap.get("mq_list");
        logger.info("######mq_list:{}",mqStr);
        if(StringUtils.isNotBlank(mqStr)){
        	String[] mqArray = mqStr.split(";");
        	for(String mqName : mqArray)
        		mqList.add(mqName);
        }
        if(isBroadType && mqList.size() == 0){
        	logger.error("广播模式，必须配置MQ列表");
        }
        
    }

//    public static ShopHeartBeatPool getPool() {
//        return pool;
//    }

    public static Object getContextProperty(String name) {
        return ctxPropertiesMap.get(name);
    }
}
